package com.joinway.framework.support.logging.utils;

import java.lang.reflect.Method;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.aspectj.lang.JoinPoint;
import org.slf4j.MDC;

import com.joinway.framework.bean.utils.MaskedReflectionToStringBuilder;
import com.joinway.framework.support.audit.constant.AuditConstants;
import com.joinway.framework.support.logging.constant.LoggingConstants;

public final class LoggingUtils {
	
    public static String getLoggerText(JoinPoint jp, Method method){
        return jp.getTarget().getClass().getCanonicalName() + "." + method.getName();
    }

    public static void putLoggerText(String loggerText){
        MDC.put(LoggingConstants.LOGGER_KEY, loggerText); 
    }
    
    public static void removeLoggerText(){
        MDC.remove(LoggingConstants.LOGGER_KEY);
    }

    public static String convertObjectToString(Object obj){
    	String text = "";
    	
    	if(obj instanceof CharSequence){
    		text = String.valueOf(obj);
    	}else{
    		text = new MaskedReflectionToStringBuilder(obj, ToStringStyle.MULTI_LINE_STYLE).toString();
    	}
    	
    	return text;
    }
    
    public static void addFlowKey(){
    	String flow = StringUtils.split(UUID.randomUUID().toString(), "-")[0];
		MDC.put(AuditConstants.FLOW_KEY, flow);
    }
    
    public static String getFlowKey(){
    	return MDC.get(AuditConstants.FLOW_KEY);
    }
    
//    public static Object buildLogMessageBody(Object arg, LogMask mask) {
//        Object body = null;
//
//        StringBuilder text = new StringBuilder();
//        
//        if (arg != null && LoggingHelper.isSimpleType(arg.getClass())) {
//            // for simple types, log as string way
//            if (mask != null) {
//                if (mask.value() == LogMaskType.SimpleMask) {
//                    // for simple mask, mark some chars
//                    text = text.append(LoggingHelper.markString(String.valueOf(arg), LogMaskType.SimpleMask.getMask()));
//                } else {
//                    // for full mask, mask all chars with mask chars
//                    text = text.append(LoggingHelper.markFullString(String.valueOf(arg), LogMaskType.FullMask.getMask()));
//                }
//            } else {
//        		text = text.append(LoggingUtils.convertObjectToString(arg));
//            }
//            body = text;
//        } else {
//            // output object
//        	if(arg instanceof BaseBean){
//        		body = arg;
//        	}else if(arg instanceof TraceableBean){
//        		body = LoggingUtils.convertObjectToString(arg);
//        	}else{
//        		body = arg;
//        	}
//        }
//        return body;
//    }
    
	private LoggingUtils(){}
}
